package ro.flashbite.game_engine.view
{
	import flash.display.Bitmap;
	import flash.display.Sprite;
	import flash.geom.Point;
	
	import ro.flashbite.game_engine.view.sheet.cpu.SimpleSpriteSheetCPU;
	import ro.flashbite.helpers.MathHelpers;
	import ro.flashbite.interfaces.IDestroyable;
	
	/**
	 * Base class used to show loading percentage of a level or the application
	 * 
	 * v 1.0
	 *  
	 * @author Adrian Barbu
	 */	
	public class LoadingScreen extends Sprite implements IDestroyable
	{
		/**
		 * background 
		 */		
		private var _background			:	Bitmap;
		/**
		 * animation of loading 
		 */		
		private var _percentAnimSprite	:	SimpleSpriteSheetCPU;
		
		// ================================================================================================================================
		// CONSTRUCTOR
		// ================================================================================================================================
		
		public function LoadingScreen(background:Bitmap, percentAnimSprite:SimpleSpriteSheetCPU, pointAnim:Point)
		{
			_constructor(background, percentAnimSprite, pointAnim);
		}
		private function _constructor(background:Bitmap, percentAnimSprite:SimpleSpriteSheetCPU, pointAnim:Point):void
		{
			//save datas
			_background = background;
			_percentAnimSprite = percentAnimSprite;
			
			//put items on this
			this.addChild(_background);
			this.addChild(_percentAnimSprite);
			// set anim location
			_percentAnimSprite.x = pointAnim.x; _percentAnimSprite.y = pointAnim.y;
			//disable interaction on this
			this.mouseEnabled = this.mouseChildren = false;
		}
		
		// ================================================================================================================================
		// PUBLIC
		// ================================================================================================================================
		
		public function update(percent:Number):void
		{
			//percent below 1
			if (percent > 1) percent = 1;
			//stop on frame
			_percentAnimSprite.gotoAndStop(Math.max(1, uint(_percentAnimSprite.totalFrames * percent)));
		}
		
		public function reset():void
		{
			_percentAnimSprite.gotoAndStop(1);
		}
		
		// ================================================================================================================================
		// DESTRUCTOR
		// ================================================================================================================================
		
		public function destroy():void
		{
			if (_background) {
				if (_background.parent) _background.parent.removeChild(_background);
				_background = null;
			}
			if (_percentAnimSprite) {
				if (_percentAnimSprite.parent) _percentAnimSprite.parent.removeChild(_percentAnimSprite);
				_percentAnimSprite = null;
			}
		}
	}
}